<?php 
/**
 * 新闻管理
 * @author juxuan
 *
 */
class NewsAdminAction extends Action{
	
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION['admin_id'])){
			header("Content-Type:text/html; charset=utf-8");
			$this->redirect('Index/login',null,3,"非法登录,3秒后为你跳转到登录页面..");
		}
	}
    
	
	//新闻管理首页：新闻列表；分页；每页显示条数：15
	public function index(){
		
		import("ORG.Util.Page");
    	$News = new Model('News');
    	
    	//所有类型的新闻
    	$count = $News->count();
    	$Page = new Page($count, 15);
    	$show = $Page->show();
    	$list = $News->order('news_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('newsList',$list);
    	$this->assign('page',$show);  
    	
    	
    	$this->display();
    }
    

    public function add(){
    	$this->display();
    }
    
    
    /**
     +----------------------------------------------------------
     * 添加新闻的表单处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    public function insertNews(){
    	
    	
  		//如果上传附件，则将附件上传
    	//if(!empty($_FILES)){
    	
    	if(isset($_POST['isFile'])){	
			import("ORG.Net.UploadFile");	//导入文件上传类	
	    	$upload = new UploadFile();		//实例化上传类
	    	$upload->savePath = 'Public/newsAffix/';	//设置附件上传目录
		 //   $upload->saveRule = time;	//设置上传文件规则
		    
		    if(!$upload->upload()){	//上传错误，提示错误信息
		    	header("Content-Type:text/html; charset=utf-8");
    			exit($upload->getErrorMsg().'sss[<a href="javascript:history.back()">返回</a>]');
		    	//$this->error($upload->getErrorMsg().'hhh');
		    }
		    else{
		    	$uploadList = $upload->getUploadFileInfo();	//上传成功，获取上传文件信息
		    	
		    }
		    
		    
    		$NewsForm = D("Admin.News");	//插入新闻的主体内容
    		
//    		$message = $_POST['message'];
//    		$showimg = 
    		
    		
        	if($vo = $NewsForm->create()){
	    		if($newsID = $NewsForm->add()){
	    	
    				$NewsAffix = M('Newsaffix');	//在新闻附件表插入附件信息
				    foreach ($uploadList as $vo){
				    	$affix['news_id'] = $newsID;
				    	$affix['filename'] = $vo['savename'];
				    	$affix['showname'] = $vo['savename'];
				    	$NewsAffix->add($affix);
					    
	    			}
				   
	    			header("Content-Type:text/html; charset=utf-8");
    				exit('新闻发布成功！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    			//$this->success('新闻发布成功！');	
	    		}else{
	    			header("Content-Type:text/html; charset=utf-8");
    				exit('新闻发布失败！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    			//$this->error('新闻发布失败！');
	    			
	    		}
	    	}else{
	    		header("Content-Type:text/html; charset=utf-8");
    			exit($NewsForm->getError().'[<a href="javascript:history.back()">返回</a>]');
	    		//$this->error($NewsForm->getError());
	    	}
		    
	    
    	}
    	else{	
    			//不上传附件的情况
    			$NewsForm = D("Admin.News");
    	    	if($vo = $NewsForm->create()){
		    		if($newsID = $NewsForm->add()){
		    			//$vo['message'] = nl2br($vo['message']);
		    			header("Content-Type:text/html; charset=utf-8");
		    			exit('新闻发布成功[无附件]！'.'[<a href="javascript:history.back()">返回</a>]');	
		    		}else{
//		    			$this->ajaxReturn('新闻发布失败！');
						header("Content-Type:text/html; charset=utf-8");
						exit('新闻发布失败[无附件]！'.'[<a href="javascript:history.back()">返回</a>]');
		    			
		    		}
		    	}else{
		    		header("Content-Type:text/html; charset=utf-8");
		    		exit($NewsForm->getError().'[<a href="javascript:history.back()">返回</a>]');
		    		//$this->error($NewsForm->getError());
		    	}
    	}
   		
		
	    
//    	$NewsForm = D("Admin.News");
//    	
//    	if($vo = $NewsForm->create()){
//    		if($newsID = $NewsForm->add()){
//    			$vo['message'] = nl2br($vo['message']);
//    			
//	    		/* 如果有上传附件,将附件信息保存进数据库的新闻附件表*/ 
//    			if($uploadList != null){
//    				$NewsAffix = new M('Newsaffix');
//				    foreach ($uploadList as $vo){
//				    	$affix['news_id'] = $newsID;
//				    	$affix['filename'] = $vo['savename'];
//				    	$affix['showname'] = $vo['savename'];
//				    	$NewsAffix->add($affix);
//				    }
//    			}
//			   
//    			//$this->ajaxReturn($data)
//    			//$this->success('新闻发布成功！');
//    			//$this->ajaxReturn($uploadList,'新闻发布成功！',1);
//    			
//    			
//    			header("Content-Type:text/html; charset=utf-8");
//    			exit('新闻发布成功！'.'[<a href="javascript:history.back()">返回</a>]');
//    				
//    		}else{
//    			header("Content-Type:text/html; charset=utf-8");
//    			exit('新闻发布失败！'.'[<a href="javascript:history.back()">返回</a>]');
//    			
//    			
//    			//$this->ajaxReturn('新闻发布失败！');
//    			
//    		}
//    	}else{
//    		header("Content-Type:text/html; charset=utf-8");
//		    exit($NewsForm->getError().'[<a href="javascript:history.back()">返回</a>]');
//    	//	$this->error($NewsForm->getError().'sss');
//    		
//    	}
//    	
    	
    	
    }
    
//    protected function _upload(){
//    	
//    }
    
    
     /**
     +----------------------------------------------------------
     * 更新新闻的表单处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    public function updateNews(){	//更新新闻，通过_GET方法读取new_id，以此从数据库获取该新闻的信息；进入新闻更新页面
    	   	
    	$News = new Model('News');
    	$news_id = (int)$_GET['news_id']; 	
		$news = $News->where('news_id='.$news_id)->find();
		
		$NewsAffix = new Model('Newsaffix');	//新闻的附件
		$newsAffixList = $NewsAffix->where('news_id='.$news_id)->findAll();
		
		$this->assign('news',$news);
		$this->assign('newsAffixList',$newsAffixList);
		$this->display();
    }
    
    
    public function updateCommit(){	//更新确认提交
    	
    	
    	if(isset($_POST['isFile'])){	//更新操作若有添加新的附件，则将附件上传	
			import("ORG.Net.UploadFile");	//导入文件上传类	
	    	$upload = new UploadFile();		//实例化上传类
	    	$upload->savePath = 'Public/newsAffix/';	//设置附件上传目录
		 //   $upload->saveRule = time;	//设置上传文件规则
		    
		    if(!$upload->upload()){	//上传错误，提示错误信息
		    	header("Content-Type:text/html; charset=utf-8");
    			exit($upload->getErrorMsg().'新闻更新附件上传错误！[<a href="javascript:history.back()">返回</a>]');
		    	//$this->error($upload->getErrorMsg().'hhh');
		    }
		    else{
		    	$uploadList = $upload->getUploadFileInfo();	//上传成功，获取上传文件信息
		    }
		    
    		$NewsForm = D("News");
	    	
	    	$news_id = (int)$_POST['news_id'];
	    	
	    	$NewsForm->find($news_id);
	    	$NewsForm->title = $_POST['title'];
	    	$NewsForm->subtitle = $_POST['subtitle'];
	    	$NewsForm->type = $_POST['type'];
	    	$NewsForm->message = $_POST['message'];
	    	
    		$NewsAffix = M('Newsaffix');	//在新闻附件表插入附件信息
			foreach ($uploadList as $vo){
		    	
				$affix['news_id'] = $news_id;
		    	$affix['filename'] = $vo['savename'];
		    	$affix['showname'] = $vo['savename'];
		    	$NewsAffix->add($affix);
				    
    		}
	    	
	    	
	    	if($NewsForm->save()){
	    		
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('新闻更新成功！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    		//$this->ajaxReturn(0,'新闻更新成功！',1);	
	
	    	}else{
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('新闻更新失败！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    		//$this->ajaxReturn('新闻更新失败！');
	    	}
		    
		    
    	
    	}else{	//更新操作没有添加新的附件的情况
	    	$NewsForm = D("News");
	    	
	    	$news_id = (int)$_POST['news_id'];
	    	
	    	$NewsForm->find($news_id);
	    	$NewsForm->title = $_POST['title'];
	    	$NewsForm->subtitle = $_POST['subtitle'];
	    	$NewsForm->type = $_POST['type'];
	    	$NewsForm->message = $_POST['message'];
	    	
	    	
	    	if($NewsForm->save()){
	    		//$this->ajaxReturn(0,'新闻更新成功！',1);
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('新闻更新成功！[无附件]'.'[<a href="javascript:history.back()">返回</a>]');	
	
	    	}else{
	    		//$this->ajaxReturn('新闻更新失败！');
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('新闻更新失败！[无附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    	}
	    	
    	}
    	
    	
    	
    	
    }
    
    /**
     +----------------------------------------------------------
     * 删除新闻的处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */

    //跳转到新闻删除确认询问页面
    public function deleteNews(){
    	$NewsForm = D("News");
    	$news_id = (int)$_GET['news_id'];
    	
    	$this->assign("news_id",$news_id);
    	$this->display(); 	
    }
  	
    
    //删除确认提交
    public function deleteCommit(){
    	$NewsForm = D("News");
    	$news_id = (int)$_POST['news_id'];
    	
    	$NewsForm->find($news_id);
	
    	if($NewsForm->delete()){
    		//删除新闻的附件
    		$Newsaffix = D("Newsaffix");
    		$Newsaffix->where('news_id='.$news_id)->delete();
    		
    		$this->ajaxReturn(0,'新闻删除成功！',1);	

    	}else{
    		$this->ajaxReturn('新闻删除失败！');
    	}
    	
    	
    }
    
    /**
     +----------------------------------------------------------
     * 删除新闻的附件
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */   
    
    public function deleteAffix(){
    	
    	$Newsaffix = D("Newsaffix");
    	$affix_id = (int)$_GET['affix_id'];
    	$news_id = (int)$_GET['news_id'];
    	
    	$Newsaffix->find($affix_id);
		
    	if($Newsaffix->delete()){
    		
    		$this->redirect('NewsAdmin/updateNews/news_id/'.$news_id);
    	}
 	
    }
    
    /**
     +----------------------------------------------------------
     * 管理网站首页学院要闻的滚动图片
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    public function manageShowImg(){
    	import("ORG.Util.Page");
    	$News = new Model('News');
    	//学院要闻top5
		$countTop5 = $News->where('type=0')->count();
    	$PageTop5 = new Page($countTop5, 5);
    	$showTop5 = $PageTop5->show();
    	
		$top5NewsList = $News->where('type=0')->order('news_id desc')->limit($PageTop5->firstRow.','.$PageTop5->listRows)->select();
		$this->assign("top5NewsList",$top5NewsList);
		$this->assign('pageTop5',$showTop5);
		
		$this->display();
    }
    
    //进入学院要闻展示图片设置页面
    public function setShowImg(){
    	
    	$News = new Model('News');
    	$news_id = (int)$_GET['news_id'];
    	
    	$news = $News->where('news_id='.$news_id)->find();
    	$this->assign('news',$news);
    	$this->display();
    }
    
    //学院要闻展示图片提交
    public function commitShowImg(){
    	if(!empty($_FILES)) {
			$this->_upload();
		}
    }
    
    protected function _upload(){
    	
    	import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize  = 3292200 ;
        //设置上传文件类型
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath =  'Public/newsShowImage/';
	    //设置需要生成缩略图，仅对图像文件有效
       $upload->thumb =  true;
       //设置需要生成缩略图的文件后缀
	//    $upload->thumbPrefix   =  'm_';  
       //设置缩略图最大宽度
		$upload->thumbMaxWidth =  '180';
       //设置缩略图最大高度
		$upload->thumbMaxHeight = '120';
	   //设置上传文件规则
	   $upload->saveRule = time;
	   //删除原图
	   $upload->thumbRemoveOrigin = true;
	   
        if(!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        }else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            //$_POST['image']  = $uploadList[0]['savename'];
        }
        
        //保存当前数据对象
        $News = M ('News'); 
        $news_id = $_POST['news_id'];
        $News->find($news_id);
        $News->showImage = 'thumb_'.$uploadList[0]['savename'];
        
        $list=$News->save();
        if($list!==false){
            $this->success ('上传展示图片成功！');
        }else{
            $this->error ('上传展示图片失败!');
        }
	}
    
    
    
}
?>